package com.socialbank.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wqdata.auth.bean.Role;
import com.wqdata.auth.bean.User;
import com.wqdata.auth.bean.UserRole;
import com.wqdata.auth.dto.UserDto;
import com.wqdata.auth.service.RoleService;
import com.wqdata.auth.service.UserService;
import com.wqdata.auth.util.PageParam;
import com.wqdata.auth.util.PageUtils;
import com.wqdata.auth.util.ShirlDm5Util;
import com.wqdata.auth.util.UserUtils;
import com.wqdata.auth.vo.UserVo;

@Controller
@RequestMapping("/sys/user")
public class UserController{
	
	private static Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/toUserList")
	public String toUserList(){
		return "systemConfig/UserList";
	}
	
	@RequestMapping("/toResetPassword")
	public String toResetPassword(){
		return "systemConfig/ResetPassword";
	}
	
	
	
	//分页查询用户列表
	@RequestMapping(value = "/getUserList",method=RequestMethod.GET)
	public @ResponseBody Map<String, Object> getUserList(PageParam pageParam){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		UserDto userDto = new UserDto();
		List<UserDto> userList = new ArrayList<>();
		PageInfo<UserDto> pageInfo = userService.getUserDtoListPage(userDto, pageParam);
		userList = pageInfo.getList();
		resultMap.put("userList", userList);
		//获取分页条的java代码
		String pageStr = PageUtils.pageStr(pageInfo, "userMgr.getUserListPage");
		resultMap.put("pageStr", pageStr);
		return 	resultMap;	
	}
	
	@RequestMapping("toUserEdit")
	@RequiresPermissions(value = { "user:add", "user:update" }, logical = Logical.OR)
	public String toUserEdit(@ModelAttribute("editFlag") int editFlag,Long userId,Model model){
		//不管是进入修改页面还是增加页面,都要将所有的角色列表查询出来
		List<Role> roleList = roleService.getRoleList();
		model.addAttribute("roleList", roleList);
		//1 为增加 2为修改
		if(editFlag == 2){
			//User user = userService.getUserById(userId);
			UserDto userDto = userService.getUserInfoById(userId);
			model.addAttribute("backUser", userDto);
			//查询当前用户的角色信息
			List<UserRole> userRoles = userService.getUserRoleByUserId(userId);
			if(userRoles.size() > 0 && userRoles != null){
				Map<Long, Long> roleCheckMap = new HashMap<>();
				for(UserRole userRole : userRoles){
					roleCheckMap.put(userRole.getRoleId(), userRole.getRoleId());
				}		
				model.addAttribute("roleCheckMap", roleCheckMap);
			}
		}
		return "systemConfig/UserAdd";
	}
	
	@RequestMapping(value = "saveUser",method=RequestMethod.POST)
	@RequiresPermissions("user:add")
	public @ResponseBody Map<String, Object> saveUser(@RequestBody UserVo userVo){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
				userService.addUserVo(userVo);
				resultMap.put("code", 200);
				resultMap.put("message", "增加用户信息成功");
		} catch (Exception e) {
			logger.error("操作用户失败",e);
		}
		
		return resultMap;
	}
	@RequestMapping(value = "updateUser",method=RequestMethod.POST)
	@RequiresPermissions("user:update")
	public @ResponseBody Map<String, Object> updateUser(@RequestBody UserVo userVo){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
				userService.updateUserVo(userVo);
				resultMap.put("code", 200);
				resultMap.put("message", "修改用户信息成功");
		} catch (Exception e) {
			logger.error("操作用户失败",e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "updatePassword",method=RequestMethod.POST)
	@RequiresPermissions("user:update")
	public @ResponseBody Map<String, Object> updatePassword(String oldPassword,String newPassword){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Long userId = UserUtils.getCurrrentUserId();
		User user = userService.getUserById(userId);
		//获取原密码
		String password = ShirlDm5Util.encrypt(oldPassword, user.getSalt());
		//比对密码
		if(user.getPassword().equals(password)){
			try {
				boolean flag = userService.updateUserPassword(userId, ShirlDm5Util.encrypt(newPassword, user.getSalt()));
				if(flag){
					resultMap.put("code", 200);
					resultMap.put("result", "修改密码成功");
				}else{
					resultMap.put("code", 400);
					resultMap.put("result", "修改密码失败");
				}
			} catch (Exception e) {
				resultMap.put("code", 400);
				resultMap.put("result", "修改密码失败");
				logger.error("修改密码失败",e);
			}
		}else{
			resultMap.put("code", 400);
			resultMap.put("result", "修改密码失败,原密码错误");
		}
		return resultMap;
	}
}
